﻿/**********************************天使物流*******************************************
* 功能描述:物资管理——物资采购
* 作者:孟帅龙
* 时间:2022/1/10
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.Materials.PurchaseRequests;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.Materials.PurchaseRequests
{
    public class PurchaseRequestRepository : DapperDbContextRepository, IPurchaseRequestRepository
    {
        public PurchaseRequestRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 物资采购——查询分页显示
        /// <summary>
        /// 物资采购——查询分页显示
        /// </summary>
        /// <param name="GoodsName"></param>
        /// <param name="Texture"></param>
        /// <param name="Nationw"></param>
        /// <param name="Province"></param>
        /// <param name="Applicatp"></param>
        /// <param name="AuditStatep"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<PurchaseRequestDto>> GetPurchaseRequestPageShow(string GoodsName,int Texture, int Nationw, int Province,string Applicatp,int AuditStatep, int pageindex = 1, int pagesize = 3)
        {
            PageResponse<PurchaseRequestDto> response = new PageResponse<PurchaseRequestDto>();
            var sql = "select a.Id,a.PurchaseApplyNumber,a.GoodsName,a.Typey,a.Texture,a.Specification,a.Nationw,a.Province,a.Numbers,a.ExpectDeliveryTime,a.UseDescribes,a.Remarkm,a.Applicatp,a.EstablishTimez,a.AuditStatep,a.Approverp,a.IsDelete,b.DictionaryName as BName,c.DictionaryName as CName,d.CountryName as DName,e.CountryName as EName,f.DictionaryName as FName,g.OperatorName as GName from PurchaseRequest a join Dictionary b on a.Typey=b.Id join Dictionary c on a.Texture=c.Id join Country d on a.Nationw=d.Id join Country e on a.Province=e.Id join Dictionary f on a.AuditStatep=f.Id join Operator g on a.Applicatp=g.OperatorName where a.IsDelete=0";
            if (!string.IsNullOrWhiteSpace(GoodsName))
            {
                sql += $" and a.GoodsName like '%" + GoodsName + "%'";
            }
            if (Texture > 0)
            {
                sql += $" and a.Texture='{Texture}'";
            }
            if (Nationw > 0)
            {
                sql += $" and a.Nationw='{Nationw}'";
            }
            if (Province > 0)
            {
                sql += $" and a.Province='{Province}'";
            }
            if (!string.IsNullOrWhiteSpace(Applicatp))
            {
                sql += $" and a.Applicatp like '%" + Applicatp + "%'";
            }
            if (AuditStatep > 0)
            {
                sql += $" and a.AuditStatep='{AuditStatep}'";
            }
            var list = await this.QueryAsync<PurchaseRequestDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<PurchaseRequestDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 物资采购——逻辑删除
        /// <summary>
        /// 物资采购——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeletePurchaseRequest(int id)
        {

            var sql = $"update PurchaseRequest set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        #endregion

        #region 物资采购——批量删除
        /// <summary>
        /// 物资采购——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteBatchPurchaseRequest(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update PurchaseRequest set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        #endregion

        #region 物资采购——编辑
        /// <summary>
        /// 物资采购——编辑
        /// </summary>
        /// <param name="purchaseRequestDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdatePurchaseRequest(PurchaseRequestDto purchaseRequestDto)
        {
            var sql = $"update PurchaseRequest set PurchaseApplyNumber='{purchaseRequestDto.PurchaseApplyNumber}',GoodsName='{purchaseRequestDto.GoodsName}',Typey='{purchaseRequestDto.Typey}',Texture='{purchaseRequestDto.Texture}',Specification='{purchaseRequestDto.Specification}',Nationw='{purchaseRequestDto.Nationw}',Province='{purchaseRequestDto.Province}',Numbers='{purchaseRequestDto.Numbers}',ExpectDeliveryTime='{purchaseRequestDto.ExpectDeliveryTime}',UseDescribes='{purchaseRequestDto.UseDescribes}',Remarkm='{purchaseRequestDto.Remarkm}',Applicatp='{purchaseRequestDto.Applicatp}',EstablishTimez='{purchaseRequestDto.EstablishTimez}',AuditStatep='{purchaseRequestDto.AuditStatep}',Approverp='{purchaseRequestDto.Approverp}',IsDelete='{purchaseRequestDto.IsDelete}' where Id='{purchaseRequestDto.Id}'";
            return await ExecuteAsync(sql, purchaseRequestDto);
        }
        #endregion

        #region 物资采购——添加
        /// <summary>
        /// 物资采购——添加
        /// </summary>
        /// <param name="purchaseRequestDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddPurchaseRequest(PurchaseRequestDto purchaseRequestDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into PurchaseRequest values('{id}','{purchaseRequestDto.GoodsName}','{purchaseRequestDto.Typey}','{purchaseRequestDto.Texture}','{purchaseRequestDto.Specification}','{purchaseRequestDto.Nationw}','{purchaseRequestDto.Province}','{purchaseRequestDto.Numbers}','{purchaseRequestDto.ExpectDeliveryTime}','{purchaseRequestDto.UseDescribes}','{purchaseRequestDto.Remarkm}','张三1',getdate(),16,'1',0)";
            return await ExecuteAsync(sql, purchaseRequestDto);
        }
        #endregion
    }
}
